package inf.coach.mobile;

import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView.OnEditorActionListener;

public class Stationanno extends Activity {
	
    public static long userid = 0;
    public static long osmid= 0;
    public static double lat;
    public static double lon;
    
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stationanno);
       setTitle("funktion Seite, Haltestelle Informationen ");
        
       	final Bundle bundle = this.getIntent().getExtras();  
		userid = bundle.getLong("userid");
		osmid= bundle.getLong("osmid");
		lat=bundle.getDouble("lat");
		lon=bundle.getDouble("lon");
		
		final boolean flag =bundle.getBoolean("flag");
		
        final Button send=(Button) findViewById(R.id.button_station_absenden);
        final Button direction=(Button) findViewById(R.id.button_station_direction);
        final TextView name=(TextView) findViewById(R.id.autoCompleteTextView_station_streetname);
        final CheckBox bus=(CheckBox)findViewById(R.id.checkBox_bus);
        final CheckBox str=(CheckBox)findViewById(R.id.checkBox_str);        
        final CheckBox dou=(CheckBox)findViewById(R.id.checkBox_double);
        final CheckBox shed=(CheckBox)findViewById(R.id.checkBox_shed);
		
		if(flag)
		{

			name.setText(bundle.getString("line"));
			name.setEnabled(false);
			bus.setChecked(bundle.getBoolean("bus"));
			bus.setClickable(false);
			str.setChecked(bundle.getBoolean("str"));
			str.setClickable(false);
			dou.setChecked(bundle.getBoolean("doppel"));
			dou.setClickable(false);
			shed.setChecked(bundle.getBoolean("shed"));
			shed.setClickable(false);			
			
		}
		else
		{
			send.setText("Absenden");
			send.setContentDescription("Button, absenden ");
			direction.setEnabled(false);
		}
		
     
        send.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View arg0){
        		if(flag && send.getText().equals("Bearbeiten"))
        		{
        			name.setEnabled(true);
        			bus.setClickable(true);
        			str.setClickable(true);
        			dou.setClickable(true);
        			shed.setClickable(true);
        			send.setText("Absenden");
        			send.setContentDescription("Button, absenden ");
        		}
        		else if(flag && send.getText().equals("Absenden"))
        		{
            		if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
    				{
    									
    				String text = webservice.updateStationAnno( userid,  osmid, lat,lon,name.getText().toString(),bus.isChecked(),str.isChecked(), dou.isChecked(),shed.isChecked());
    				System.out.println(text);
    				if (!text.equals(null) && text.equals("successful")) {
    					dialog();

    				}
    				if (text.equals(null) || text.equals("failed")) {
    					message.show(Stationanno.this,"Fehlgeschlagen", "Bitte überprüfen Internetverbindung!");
    				}
    				}
            			else{
        					message.show(Stationanno.this,"Fehler", "Keine Internetverbindung! ");
        				}
        		}
        		else
        		{
        		if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
				{
									
				String text = webservice.createStationAnno( userid,  osmid, lat,lon,name.getText().toString(),bus.isChecked(),str.isChecked(), dou.isChecked(),shed.isChecked());
				System.out.println(text);
				if (!text.equals(null) && text.equals("successful")) {
					dialog();

				}
				if (text.equals(null) || text.equals("failed")) {
					message.show(Stationanno.this,"Fehlgeschlagen", "Bitte überprüfen Internetverbindung!");
				}
				}
        			else{
    					message.show(Stationanno.this,"Fehler", "Keine Internetverbindung!");
    				}
        		}
        	}
        	
        });
       direction.setOnClickListener(new Button.OnClickListener(){
    	   public void onClick(View arg0){
    		   if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
				{
    			   Intent intent = new Intent();
					Bundle bundle = new Bundle();
					bundle.putDouble("lat", lat);
					bundle.putDouble("lon",lon);
					intent.setClass(Stationanno.this, Orientation.class);
					intent.putExtras(bundle);
					startActivity(intent);
				}
    		   else
    		   {
    			   message.show(Stationanno.this,"Fehler", "Keine Internetverbindung! ");
    		   }
       }
       });
       
     
	}
	
	protected void dialog() { 
        AlertDialog.Builder builder = new Builder(Stationanno.this); 
        builder.setMessage("Automatisch zur letzten Seite zurück!"); 
        builder.setTitle("Erfolg"); 
        builder.setPositiveButton("ok", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                        Stationanno.this.finish();
                    }

                }); 
        
        builder.create().show(); 
    } 
	 protected void dialoggoback() { 
	        AlertDialog.Builder builder = new Builder(Stationanno.this); 
	        builder.setMessage("Möchten Sie zur letzten Seite zurück springen?"); 
	        builder.setTitle("Information"); 
	        builder.setPositiveButton("ok", 
	                new android.content.DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                        dialog.dismiss(); 
	                        Stationanno.this.finish(); 
	                    }

	                }); 
	        builder.setNegativeButton("abbrechen", 
	                new android.content.DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                        dialog.dismiss(); 
	                    } 
	                }); 
	        builder.create().show(); 
	    } 
	    @Override 
	    public boolean onKeyDown(int keyCode, KeyEvent event) { 
	        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
	            dialoggoback(); 
	            return false; 
	        } 
	        return false; 
	    } 
	
}
